<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品管理</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        :root {
            --primary-color: #ffd700;
            --primary-light: #fff9e6;
            --text-color: #333;
            --border-color: #ffe066;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: var(--primary-light);
        }

        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 15px;
        }

        .header {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .header h1 {
            color: var(--text-color);
            font-size: 24px;
        }

        .back-btn {
            padding: 8px 16px;
            background-color: var(--primary-color);
            color: var(--text-color);
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .back-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 2px 8px rgba(255, 215, 0, 0.3);
        }

        .tab-container {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .tabs {
            display: flex;
            border-bottom: 2px solid var(--border-color);
            margin-bottom: 20px;
        }

        .tab {
            padding: 12px 24px;
            cursor: pointer;
            position: relative;
            color: var(--text-color);
            font-weight: 500;
            transition: all 0.3s;
        }

        .tab:hover {
            color: var(--primary-color);
        }

        .tab.active {
            color: var(--primary-color);
        }

        .tab.active::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 100%;
            height: 2px;
            background: var(--primary-color);
        }

        .product-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            padding: 10px;
        }

        .product-card {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            transition: all 0.3s;
            border: 1px solid var(--border-color);
        }

        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(255, 215, 0, 0.2);
        }

        .product-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
        }

        .product-info {
            padding: 15px;
        }

        .product-name {
            font-size: 16px;
            color: var(--text-color);
            margin-bottom: 10px;
            height: 40px;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }

        .product-price {
            color: #ff4400;
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .product-stats {
            display: flex;
            justify-content: space-between;
            color: #666;
            font-size: 13px;
            margin: 10px 0;
            padding: 5px 0;
            border-top: 1px solid #eee;
        }

        .status-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            margin-bottom: 10px;
        }

        .status-upcoming {
            background-color: #e6f7ff;
            color: #1890ff;
        }

        .status-ongoing {
            background-color: #f6ffed;
            color: #52c41a;
        }

        .status-ended {
            background-color: #fff1f0;
            color: #ff4d4f;
        }

        .product-actions {
            display: flex;
            gap: 8px;
            margin-top: 15px;
        }

        .action-btn {
            flex: 1;
            padding: 8px 0;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 5px;
        }

        .action-btn i {
            font-size: 12px;
        }

        .edit-btn {
            background: var(--primary-color);
            color: var(--text-color);
        }

        .edit-btn:hover {
            background: #ffe033;
        }

        .status-btn {
            background: #ff4400;
            color: white;
        }

        .status-btn:hover {
            background: #ff5500;
        }

        .delete-btn {
            background: #f5f5f5;
            color: #666;
        }

        .delete-btn:hover {
            background: #ff4444;
            color: white;
        }

        .empty-state {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .empty-state i {
            font-size: 48px;
            color: var(--primary-color);
            margin-bottom: 20px;
        }

        .status-auction {
            background: #e6f7ff;
            color: #1890ff;
            border: 1px solid #91d5ff;
        }

        .auction-card {
            border: 1px solid #91d5ff;
            background: linear-gradient(to bottom right, #ffffff, #f0f9ff);
        }

        .auction-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 10px 0;
            padding: 8px 12px;
            background: rgba(24, 144, 255, 0.05);
            border-radius: 4px;
            font-size: 13px;
            color: #666;
        }

        .bid-count {
            color: #1890ff;
            font-weight: 500;
            font-size: 14px;
        }

        .end-time {
            color: #666;
            font-size: 13px;
            text-align: right;
        }

        .product-price div {
            margin: 5px 0;
        }

        .action-btn.disabled {
            opacity: 0.5;
            cursor: not-allowed;
            background: #f5f5f5;
            color: #999;
        }

        .tooltip-text {
            position: absolute;
            background-color: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 6px 10px;
            border-radius: 4px;
            font-size: 12px;
            white-space: nowrap;
            z-index: 1000;
            top: -30px;
            left: 50%;
            transform: translateX(-50%);
            display: none;
        }

        .tooltip-text::after {
            content: '';
            position: absolute;
            top: 100%;
            left: 50%;
            margin-left: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: rgba(0, 0, 0, 0.8) transparent transparent transparent;
        }

        .action-btn[disabled] {
            opacity: 0.5;
            cursor: not-allowed;
        }

        .tooltip {
            position: relative;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <button class="back-btn" onclick="location.href='mine.html'">
                <i class="fas fa-arrow-left"></i> 返回
            </button>
            <h1>商品管理</h1>
            <button class="back-btn" onclick="location.href='publish.html'">
                <i class="fas fa-plus"></i> 发布商品
            </button>
        </div>

        <div class="tab-container">
            <div class="tabs">
                <div class="tab active" data-status="on_sale">在售商品</div>
                <div class="tab" data-status="auction">拍卖商品</div>
                <div class="tab" data-status="off_shelf">已下架</div>
            </div>

            <div class="product-list" id="productList">
                <!-- 商品列表将通过JavaScript动态填充 -->
            </div>
        </div>
    </div>

    <script>
        let currentStatus = 'on_sale';

        function showEmptyState(message) {
            const productList = $('#productList');
            productList.html(`
                <div class="empty-state">
                    <i class="fas fa-box-open"></i>
                    <p>${message}</p>
                </div>
            `);
        }

        function getStatusClass(status) {
            switch(status) {
                case 1: return 'status-on-sale';
                case 2: return 'status-off-shelf';
                case 0: return 'status-deleted';
                default: return '';
            }
        }

        function getStatusText(status) {
            switch(status) {
                case 1: return '在售';
                case 2: return '已下架';
                case -1: return '已删除';
                default: return '未知状态';
            }
        }

        function getActionButton(product) {
            if (product.status === 1) {
                return `
                    <button class="action-btn status-btn" onclick="changeStatus(${product.id}, 2)">
                        <i class="fas fa-archive"></i> 下架
                    </button>
                `;
            } else if (product.status === 2) {
                return `
                    <button class="action-btn status-btn" onclick="changeStatus(${product.id}, 1)">
                        <i class="fas fa-shop"></i> 上架
                    </button>
                `;
            }
            return '';
        }

        function displayProducts(products, isAuction = false) {
            const productList = $('#productList');
            productList.empty();

            if (!products || products.length === 0) {
                showEmptyState(currentStatus === 'on_sale' ? 
                    '暂无在售商品，快去发布吧！' : 
                    currentStatus === 'auction' ? 
                    '暂无拍卖商品，快去发布吧！' : '暂无下架商品');
                return;
            }

            products.forEach(product => {
                let images = [];
                try {
                    images = JSON.parse(product.images || '[]');
                } catch (e) {
                    console.error('解析商品图片失败:', e);
                }
                const mainImage = images[0] || 'default-product-image.jpg';
                
                let productCard;
                if (isAuction) {
                    // 拍卖商品卡片模板
                    const now = new Date().getTime();
                    const startTime = new Date(product.startTime).getTime();
                    const endTime = new Date(product.endTime).getTime();
                    
                    let statusText = '即将开始';
                    let statusClass = 'status-upcoming';
                    let canEdit = true;
                    
                    if (now >= startTime && now <= endTime) {
                        statusText = '拍卖中';
                        statusClass = 'status-ongoing';
                        canEdit = false;
                    } else if (now > endTime) {
                        statusText = '已结束';
                        statusClass = 'status-ended';
                        canEdit = false;
                    }
                    
                    productCard = `
                        <div class="product-card">
                            <img src="${product.firstImage}" alt="${product.productName}" class="product-image" 
                                 onclick="location.href='auction-detail.html?id=${product.id}'" 
                                 style="cursor: pointer;">
                            <div class="product-info">
                                <div class="status-badge ${statusClass}">
                                    ${statusText}
                                </div>
                                <h3 class="product-name" onclick="location.href='auction-detail.html?id=${product.id}'" 
                                    style="cursor: pointer;">${product.productName}</h3>
                                <div class="product-price">¥${product.currentPrice}</div>
                                <div class="auction-info">
                                    <div class="bid-count">
                                        <i class="far fa-gavel"></i> ${product.bidCount || 0}次出价
                                    </div>
                                    <div class="end-time">
                                        <i class="far fa-clock"></i> ${new Date(product.endTime).toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' })} ${new Date(product.endTime).toLocaleTimeString('zh-CN', {hour: '2-digit', minute:'2-digit'})}
                                    </div>
                                </div>
                                <div class="product-actions">
                                    <button class="action-btn edit-btn" onclick="editAuction(${product.id})" 
                                        ${!canEdit ? 'disabled title="拍卖开始后不可编辑"' : ''}>
                                        <i class="fas fa-edit"></i> 编辑
                                    </button>
                                    <button class="action-btn delete-btn" onclick="deleteAuction(${product.productId})"
                                        ${!canEdit ? 'disabled title="拍卖开始后不可删除"' : ''}>
                                        <i class="fas fa-trash"></i> 删除
                                    </button>
                                </div>
                            </div>
                        </div>
                    `;
                } else {
                    // 普通商品卡片模板
                    productCard = `
                        <div class="product-card">
                            <img src="${mainImage}" alt="${product.name}" class="product-image" 
                                 onclick="location.href='product-detail.html?id=${product.id}'"
                                 style="cursor: pointer;"
                                 onerror="this.src='default-product-image.jpg'">
                            <div class="product-info">
                                <div class="status-badge ${getStatusClass(product.status)}">
                                    ${getStatusText(product.status)}
                                </div>
                                <h3 class="product-name" onclick="location.href='product-detail.html?id=${product.id}'"
                                    style="cursor: pointer;">${product.name}</h3>
                                <div class="product-price">¥${product.price.toFixed(2)}</div>
                                <div class="product-stats">
                                    <span><i class="far fa-eye"></i> ${product.viewCount || 0}</span>
                                    <span><i class="far fa-heart"></i> ${product.favoriteCount || 0}</span>
                                </div>
                                <div class="product-actions">
                                    <button class="action-btn edit-btn" onclick="editProduct(${product.id})">
                                        <i class="fas fa-edit"></i> 编辑
                                    </button>
                                    ${getActionButton(product)}
                                    <button class="action-btn delete-btn" onclick="deleteProduct(${product.id})">
                                        <i class="fas fa-trash"></i> 删除
                                    </button>
                                </div>
                            </div>
                        </div>
                    `;
                }
                productList.append(productCard);
            });
        }

        function editProduct(productId) {
            location.href = `product-edit.html?id=${productId}`;
        }

        function changeStatus(productId, newStatus) {
            $.ajax({
                url: '/seller/product/update-status',
                type: 'POST',
                data: {
                    id: productId,
                    status: newStatus
                },
                success: function(response) {
                    if (response.state === 1) {
                        loadProducts(currentStatus);
                    } else {
                        alert(response.msg || '操作失败');
                    }
                },
                error: function() {
                    alert('操作失败，请重试');
                }
            });
        }

        function deleteProduct(productId) {
            if (confirm('确定要删除该商品吗？此操作不可恢复')) {
                $.ajax({
                    url: '/seller/product/delete',
                    type: 'POST',
                    data: { id: productId },
                    success: function(response) {
                        if (response.state === 1) {
                            loadProducts(currentStatus);
                        } else {
                            alert(response.msg || '删除失败');
                        }
                    },
                    error: function() {
                        alert('删除失败，请重试');
                    }
                });
            }
        }

        function editAuction(auctionId) {
            location.href = `auction-edit.html?id=${auctionId}`;
        }

        function deleteAuction(productId) {
            console.log('删除拍卖商品:', productId);
            if (confirm('确定要删除该拍卖商品吗？此操作不可恢复')) {
                $.ajax({
                    url: '/seller/product/delete',
                    type: 'POST',
                    data: { id: productId },
                    success: function(response) {
                        if (response.state === 1) {
                            loadProducts(currentStatus);
                        } else {
                            alert(response.msg || '删除失败');
                        }
                    },
                    error: function() {
                        alert('删除失败，请重试');
                    }
                });
            }
        }

        $(document).ready(function() {
            loadProducts('on_sale');

            $('.tab').click(function() {
                $('.tab').removeClass('active');
                $(this).addClass('active');
                currentStatus = $(this).data('status');
                loadProducts(currentStatus);
            });
        });

        function loadProducts(status) {
            if (status === 'auction') {
                // 加载拍卖商品
                $.ajax({
                    url: '/auction/list',
                    type: 'GET',
                    success: function(response) {
                        console.log('获取拍卖商品列表响应:', response);
                        if (response.state === 1) {
                            displayProducts(response.data, true);
                        } else {
                            showEmptyState(response.msg || '加载失败，请重试');
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('获取拍卖商品列表错误:', error);
                        showEmptyState('加载失败，请重试');
                    }
                });
            } else {
                let queryStatus = status === 'on_sale' ? 1 : 2;
                $.ajax({
                    url: '/seller/product/list',
                    type: 'GET',
                    data: { status: queryStatus },
                    success: function(response) {
                        console.log('获取商品列表响应:', response);
                        if (response.state === 1) {
                            displayProducts(response.data, false);
                        } else {
                            showEmptyState(response.msg || '加载失败，请重试');
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('获取商品列表错误:', error);
                        showEmptyState('加载失败，请重试');
                    }
                });
            }
        }

        function showTooltip(element, text) {
            // 移除所有已存在的tooltip
            $('.tooltip-text').remove();
            
            // 创建新的tooltip
            const tooltip = $('<div class="tooltip-text">' + text + '</div>');
            $(element).append(tooltip);
            tooltip.fadeIn(200);
            
            // 3秒后自动消失
            setTimeout(() => {
                tooltip.fadeOut(200, function() {
                    $(this).remove();
                });
            }, 3000);
        }
    </script>
</body>
</html> 